﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Drawer
{
    public partial class Form1 : Form
    {
        private const int Unit_length = 32;
        private int DrawStep;
        private const int Y_Max = 512;
        private const int StartPrint = 32;
        private int LastCountAdded;
        private List<byte> DataList = new List<byte>();
//         private List<byte> DataBuffer = new List<byte>();
        private Pen TablePen = new Pen(Color.FromArgb(0x00, 0x40, 0x30));
        private Pen LinesPen = new Pen(Color.FromArgb(0x00, 0xFf, 0x00));
        private bool KeyShift, KeySet, KeyExit, KeyOpen, KeyClose, KeyStepUp, KeyStepDown;
     Setting Setting = new Setting();
        //private const int X_max = Form1.
        public Form1()
        {
            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
                           ControlStyles.AllPaintingInWmPaint,
                           true);

            this.UpdateStyles();
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            String Str = "";
            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
            e.Graphics.FillRectangle(Brushes.Black, e.Graphics.ClipBounds);
//Draw Y
            for (int i = 0; i <= this.ClientRectangle.Width / Unit_length; i++)
                e.Graphics.DrawLine(TablePen, StartPrint + i * Unit_length, StartPrint, StartPrint + i * Unit_length, StartPrint + Y_Max);
//Draw X
            for (int i = 0; i <= this.ClientRectangle.Height / Unit_length; i++)
            {
                e.Graphics.DrawLine(TablePen, StartPrint, i * Unit_length, this.ClientRectangle.Width, i * Unit_length);
                Str = ((16 - i) * 16).ToString("X");
                Str = "0x" + (Str.Length == 1 ? Str + "0" : Str);
                if (i == 0)
                    Str = "0xFF";
                if(i == 17)
                    break;
                gp.AddString(Str, this.Font.FontFamily, (int)FontStyle.Regular, 14, new RectangleF(0, StartPrint + i * Unit_length - 8, 400, 50), null);
            }
            e.Graphics.DrawPath(Pens.Green, gp);
            if (DataList.Count - 1 >= (this.ClientRectangle.Width - StartPrint) / DrawStep)
            {
                DataList.RemoveRange(0, DataList.Count - (this.ClientRectangle.Width - StartPrint)/DrawStep - 1);
            }
            // = DataList.Count;
            for (int i = 0; i < DataList.Count - 1; i++)
            {
                e.Graphics.DrawLine(LinesPen, StartPrint + i * DrawStep, 17 * Unit_length - DataList[i] * 2, StartPrint + (i + 1) * DrawStep, 17 * Unit_length - DataList[i + 1] * 2);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            int height = this.ClientRectangle.Height;
            height = this.Height - height;
            int BorderWeigh = this.Width - this.ClientRectangle.Width;
            Rectangle Rect = Screen.GetWorkingArea(this);
            this.Size = new Size(Rect.Width-(Rect.Width - BorderWeigh)%Unit_length, height + Y_Max + StartPrint + Unit_length);
            Setting.ShowDialog();
            serialPort1.PortName = Setting.PortName;
            DrawStep = Setting.DrawStep;
            //timer1.Start();
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (KeyShift)
            {
                if (KeySet)
                {
                    Setting.DrawStep = DrawStep;
                    Setting.PortName = serialPort1.PortName;
                    Setting.ShowDialog();
                    Invalidate();
                    try
                    {
                        serialPort1.PortName = Setting.PortName;
                    }
                    catch (System.Exception){}
                    DrawStep = Setting.DrawStep;
                    KeySet = false;
                }
                else
                {
                    if (KeyOpen)
                    {
                        try
                        {
                            serialPort1.Open();
                            MessageBox.Show("串口打开成功，下面接收数据", "提示");
//                            DataList.Clear();
                        }
                        catch (System.Exception)
                        {
                            MessageBox.Show("串口打开失败，请检查","错误");
                        }
                        KeyOpen = false;
                    }
                    else
                    {
                        if (KeyClose)
                        {
                            KeyClose = false;
                            if (serialPort1.IsOpen)
                            {
                                serialPort1.Close();
                                MessageBox.Show("串口关闭","提示");
                            }
                        }
                        else
                        {
                            if (KeyExit)
                            {
                                Close();
                                KeyExit = false;
                            }
                            else
                            {
                                if (KeyStepUp)
                                {
                                    if(DrawStep != Setting.MaxStep)
                                        Setting.DrawStep = DrawStep++;
                                    Invalidate();
                                    KeyStepUp = false;
                                }
                                else
                                {
                                    if (KeyStepDown)
                                    {
                                        if (DrawStep != Setting.MinStep)
                                            Setting.DrawStep = DrawStep--;
                                        Invalidate();
                                        KeyStepDown = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                KeyClose = false;
                KeyOpen = false;
                KeySet = false;
                KeyExit = false;
                KeyStepUp = false;
                KeyStepDown = false;
            }
            KeyShift = false;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Shift)
                KeyShift = true;
            switch (e.KeyCode)
            {
                case Keys.S:
                    KeySet = true;
                    break;
                case Keys.E:
                    KeyExit = true;
                    break;
                case Keys.O:
                    KeyOpen = true;
                    break;
                case Keys.C:
                    KeyClose = true;
                    break;
                case Keys.Up:
                    KeyStepUp = true;
                    break;
                case Keys.Down:
                    KeyStepDown = true;
                    break;
                default:
                    break;
            }
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] data = new byte[serialPort1.BytesToRead];//定义缓冲区，因为串口事件触发时有可能收到不止一个字节
                serialPort1.Read(data,0,data.Length);
                LastCountAdded = data.Length;
                for (int i = 0; i < data.Length; i++)
                {
                    DataList.Add(data[i]);
                }
                Invalidate();
            }
            catch (System.Exception){}
        }

    }
}
